<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('./mixins/macarons') // echarts theme

import resize from './mixins/resize.js'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    }
  },
  data() {
    return {
      chart: null,
      word:""
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.axios({
        method: "GET",
        url: "https://admin.xiaoheihe.work/bapi/selectProducts",
      }).then((response) => {
        this.word = response.data
        this.initChart(this.word)
      })
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(word) {
      this.chart = echarts.init(this.$el, 'macarons')
      var option = {
        xAxis: {
          type: 'category',
          data: ['便宜货', '大众货', '高级货', '奢饰品']
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          data: word,
          type: 'bar',
          showBackground: true,
          // barWidth: '60%',
          backgroundStyle: {
            color: 'rgba(220, 220, 220, 0.8)'
          }
        }]
      }
      this.chart.setOption(option)
    }
  }
}
</script>
